#include "Pawn.h"
#include <CCSprite.h>
#include <CCTextureCache.h>
#include <CCActionInterval.h>
#include "PawnInfo.h"
#include "PlayerProfile.h"
#include "Global.h"
#include "BattleGrid.h"
#include "AnimatePacker.h"
#include "StringConvert.h"
#include "BattleField.h"

USING_NS_CC;

PawnDesc::PawnDesc()
{
	raceType = Race_Human;
	pawnType = Pawn_Normal;
	attackType = Pawn_Fight;
	battleFieldType = BattleField_B;
}

Pawn::Pawn(const PawnDesc& desc,BattleField* field)
	:m_attack(0)
	,m_hp(0)
	,m_sprite(NULL)
	,m_pawnType(desc.pawnType)
	,m_raceType(desc.raceType)
	,m_attackType(desc.attackType)
	,mPawnColor(desc.pawnColor)
	,m_battleFieldType(desc.battleFieldType)
	,m_rectSize(desc.rect)
	,m_holdTimes(2)
	,m_inAttackQueue(false)	
	,m_ownerField(field)
{
	const PawnInfo& pawnInfo = G_PawnInfo.getPawnInfo(m_raceType,m_pawnType, 0);
	m_hp = pawnInfo.m_hp;
	m_attack = pawnInfo.m_attack;
	m_holdTimes = pawnInfo.m_holdTimes;

	char name[50] = {0};
	sprintf(name, "%s_%d_%s_0.png", pawnInfo.mPawnIDStr.c_str(), int(mPawnColor), pawnInfo.mDefaultAnim.c_str());
	m_sprite = CCSprite::createWithSpriteFrameName(name);
	sprintf(name, "%s_%d_%s", pawnInfo.mPawnIDStr.c_str(), int(mPawnColor), pawnInfo.mDefaultAnim.c_str());
	m_sprite->runAction(CCRepeatForever::create(AnimatePacker::getInstance()->getSequence(name)));
	m_sprite->retain();

	if (m_pawnType == Pawn_Elite || m_pawnType == Pawn_Champion)
	{
		if (m_battleFieldType == BattleField_A)
		{
			m_sprite->setAnchorPoint(CCPoint(0.f,1.f));
		}
		else
		{
			m_sprite->setAnchorPoint(CCPoint(0.f,0.5f));
		}
	}
	else
	{
		m_sprite->setAnchorPoint(CCPoint(0.f,1.f));
	}

}

Pawn::~Pawn()
{
	if (m_sprite)
	{
		m_sprite->removeFromParentAndCleanup(true);
		m_sprite->release();
	}

	if (m_ownerField)
	{
		m_ownerField->decPawnNum();
	}
}

void Pawn::setSprite( cocos2d::CCSprite* s )
{
	cocos2d::CCNode* parent = NULL;
	if (m_sprite)
	{
		s->setPosition(m_sprite->getPosition());
		s->setAnchorPoint(m_sprite->getAnchorPoint());
		parent = m_sprite->getParent();
		m_sprite->release();
	}

	m_sprite = s;
	if (parent != NULL)
	{
		parent->addChild(m_sprite);
	}

	m_sprite->retain();
}

bool Pawn::isActionRun( int tag )const
{
	if (m_sprite)
	{
		return m_sprite->getActionByTag(tag) != NULL;
	}

	return false;
}

void Pawn::upgradeWall()
{
	if(m_pawnType != Pawn_Wall)	//change to wall
	{
		m_pawnType = Pawn_Wall;
		
		const PawnInfo& pawnInfo = G_PawnInfo.getPawnInfo(m_raceType,m_pawnType, 0);

		char name[100] = {0};
		sprintf(name, "%s_%d.png", pawnInfo.mPawnIDStr.c_str(), 1);
		m_sprite->stopAllActions();

		m_sprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name));
		m_hp = pawnInfo.m_hp;
		m_attack = pawnInfo.m_attack;
	}
	else // upgrade wall
	{
		const PawnInfo& pawnInfo = G_PawnInfo.getPawnInfo(m_raceType,m_pawnType, 0);

		bool autoRegenerate = pawnInfo.mWallInfo.mAutoRegenerate;
		int maxhp = pawnInfo.mWallInfo.mHPMax;
		int hpIncr = pawnInfo.mWallInfo.mHPIncrease;

		if(m_hp < maxhp)
		{
			m_hp += hpIncr;

			if(m_hp >= maxhp)
				m_hp = maxhp;
		}

		char name[100] = {0};
		sprintf(name, "%s_%d.png", pawnInfo.mPawnIDStr.c_str(), 2);
		m_sprite->stopAllActions();
		m_sprite->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name));
	}

}

void Pawn::changeToAttack()
{
	m_inAttackQueue = true;
	m_sprite->runAction(cocos2d::CCScaleBy::create(0.0f,0.8f));
}

bool Pawn::isWall() const
{
	return m_pawnType == Pawn_Wall;
}

void Pawn::setHP(int hp)
{
	m_hp = hp;
	checkTypeChange();
}

void Pawn::checkTypeChange()
{
	//if (isWall())
	//{
	//	const PawnInfo& pawnInfoWallOne = G_PawnInfo.getPawnInfo(m_raceType,Pawn_Wall_Level_One);
	//	const PawnInfo& pawnInfoWallTwo = G_PawnInfo.getPawnInfo(m_raceType,Pawn_Wall_Level_Two);
	//	const PawnInfo& pawnInfoWallThree = G_PawnInfo.getPawnInfo(m_raceType,Pawn_Wall_Level_Three);

	//	if (m_hp >= pawnInfoWallThree.m_hp)
	//	{
	//		if (m_pawnType == )
	//		{
	//		}
	//	}
	//	else if (m_hp >= pawnInfoWallTwo.m_hp)
	//	{
	//	}
	//	else
	//	{

	//	}
	//}
}

void Pawn::output()
{
	std::string outputStr = "[Pawn Info]: PawnType:";

	switch(m_pawnType)
	{
	case Pawn_Normal:
		outputStr += "Normal ";
		break;
	case Pawn_Elite:
		outputStr += "Elite ";
		break;
	case Pawn_Champion:
		outputStr += "Champion ";
		break;
	case Pawn_Wall:
		outputStr += "Wall ";
		break;
	default:
		outputStr += "UnKnow ";
		break;
	}

	outputStr += "RaceType:";
	switch(m_raceType)
	{
	case Race_Human:
		outputStr += "Human ";
		break;
	default:
		outputStr += "UnKnow ";
		break;
	}

	outputStr += "AttackType:";
	switch(m_attackType)
	{
	case Pawn_Fight:
		outputStr += "Fight ";
		break;
	case Pawn_Bow:
		outputStr += "Bow ";
		break;
	default:
		outputStr += "UnKnow ";
		break;
	}

	outputStr += "BattleFieldType:";
	switch(m_battleFieldType)
	{
	case BattleField_A:
		outputStr += "Field_A ";
		break;
	case BattleField_B:
		outputStr += "Field_B ";
		break;
	default:
		outputStr += "UnKnow ";
		break;
	}

	outputStr += "InAttackQueue:";
	if (m_inAttackQueue)
	{
		outputStr += "true ";
	}
	else
	{
		outputStr += "false ";
	}

	outputStr += "ColorType:";
	switch(mPawnColor)
	{
		case PawnColor_White:
			outputStr += "Color_White ";
			break;
		case PawnColor_Blue:
			outputStr += "Color_Blue ";
			break;
		case PawnColor_Yellow:
			outputStr += "Color_Yellow ";
			break;
		default:
			outputStr += "UnKnow ";
			break;
	}

	outputStr += "HP:" + StringConvert::floatToString(m_hp);

	CCLOG("%s",outputStr.c_str());
}
